package com.kedou.app;

import java.io.File;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.kedou.domain.ProjectItem;
import com.kedou.fragment.ProjectIntroductionFragment;
import com.kedou.fragment.TeamIntroductionFragment;
import com.kedou.net.PostFollowProject;
import com.kedou.net.PostUnFollowProject;
import com.kedou.util.DebugUtil;
import com.kedou.util.NetworkUtil;
import com.kedou.util.PreferencesUtil;
import com.kedou.util.Util;

/**
 * 项目详情 界面
 * 
 * @author zhouchao
 * 
 */
public class ProjectDetailActivity extends FragmentActivity implements
		OnClickListener {
	/**
	 * 左侧 返回按钮
	 */
	private Button mBtnBack;
	/**
	 * 中间 Title
	 */
	private TextView mTvTitle;
	/**
	 * 右侧 设置
	 */
	private ImageView mImgShare;
	/**
	 * 中间Fragment
	 */
	private Fragment mContent;
	/**
	 * 项目介绍
	 */
	private Button mBtnProjectIntroduction;
	/**
	 * 团队介绍
	 */
	private Button mBtnTeamIntroduction;
	/**
	 * 领投人点评
	 */
	private Button mBtnInvestorComment;
	/**
	 * 投资方案
	 */
	private Button mBtnInvestmentProgram;
	/**
	 * 相关资料
	 */
	private Button mBtnRelatedInformation;
	/**
	 * 关注
	 */
	private Button mBtnAttention;

	/**
	 * 认筹
	 */
	private Button mBtnRenchou;
	private Context mContext;
	private ProjectItem mProjectItem;
	private Bundle mBundle;
	private ToFragment mToFragment;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		mBundle = getIntent().getBundleExtra("project");
		if (mBundle != null) {
			mProjectItem = mBundle.getParcelable("project_item");
		}
		mContext = this;
		setContentView(R.layout.activity_projectdetail);
		initView();
		setListener();
		setText();
		switchContent(new ProjectIntroductionFragment());
	}

	@Override
	public void onAttachFragment(Fragment fragment) {
		super.onAttachFragment(fragment);
		try {
			mToFragment = (ToFragment) fragment;
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 初始化View
	 */
	private void initView() {
		mBtnBack = (Button) findViewById(R.id.btn_back);
		mBtnBack.setVisibility(View.VISIBLE);
		mTvTitle = (TextView) findViewById(R.id.title);
		mTvTitle.setText(R.string.title_projectdetail);
		mImgShare = (ImageView) findViewById(R.id.img_share);
		mImgShare.setVisibility(View.VISIBLE);
		mBtnProjectIntroduction = (Button) findViewById(R.id.btn_one);
		mBtnTeamIntroduction = (Button) findViewById(R.id.btn_two);
		mBtnInvestorComment = (Button) findViewById(R.id.btn_three);
		mBtnInvestmentProgram = (Button) findViewById(R.id.btn_four);
		mBtnRelatedInformation = (Button) findViewById(R.id.btn_five);
		mBtnAttention = (Button) findViewById(R.id.btn_attention);
		mBtnRenchou = (Button) findViewById(R.id.btn_renchou);
	}

	/**
	 * 设置监听器
	 */
	private void setListener() {
		mBtnBack.setOnClickListener(this);
		mBtnProjectIntroduction.setOnClickListener(this);
		mBtnTeamIntroduction.setOnClickListener(this);
		mBtnInvestorComment.setOnClickListener(this);
		mBtnInvestmentProgram.setOnClickListener(this);
		mBtnRelatedInformation.setOnClickListener(this);
		mBtnAttention.setOnClickListener(this);
		mBtnRenchou.setOnClickListener(this);
		mImgShare.setOnClickListener(this);
	}

	/**
	 * 设置显示文本等
	 */
	private void setText() {
		DebugUtil.printE("----------hasFollowed", mProjectItem.getHasFollowed()
				+ "   " + mProjectItem.getProjectName());
		switch (Integer.valueOf(mProjectItem.getProjectStatus())) {
		case 0:
			mBtnRenchou.setText(getResources().getString(R.string.renchou));
			break;
		case 1:
			mBtnRenchou.setText(getResources().getString(R.string.pay));
			break;
		case 2:
			mBtnRenchou.setBackgroundColor(Color.GRAY);
			break;
		case 3:
			mBtnRenchou.setBackgroundColor(Color.GRAY);
			break;
		}
		switch (mProjectItem.getHasFollowed()) {
		case 0:
			mBtnAttention.setText(getResources().getString(
					R.string.tab_title_attention));
			break;

		default:
			mBtnAttention.setText(getResources().getString(
					R.string.cancel_attention));
			break;
		}
	}

	/**
	 * 点击切换中间内容
	 */
	public void switchContent(Fragment fragment) {
		mContent = fragment;
		mContent.setArguments(mBundle);
		getSupportFragmentManager().beginTransaction()
				.replace(R.id.content, mContent).commit();
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btn_back:
			finish();
			break;
		case R.id.img_share:
			shareMsg("ProjectDetailActivity", "标题", "内容",
					"/storage/emulated/0/id_image.jpg");
			break;
		case R.id.btn_one:
			backColor(0);
			switchContent(new ProjectIntroductionFragment());
			break;
		case R.id.btn_two:
			backColor(1);
			switchContent(new TeamIntroductionFragment());
			break;
		case R.id.btn_three:
			backColor(2);
			break;
		case R.id.btn_four:
			backColor(3);
			break;
		case R.id.btn_five:
			backColor(4);
			break;
		case R.id.btn_attention:
			// 关注
			if (getResources().getString(R.string.tab_title_attention).equals(
					mBtnAttention.getText())) {
				if (NetworkUtil.isNetworkAvailable(ProjectDetailActivity.this)) {
					new PostFollowProject(PreferencesUtil.getString(
							ProjectDetailActivity.this, "token"),
							mProjectItem.getProjectId(),
							PreferencesUtil.getString(
									ProjectDetailActivity.this, "userId"),
							Config.SOURCE,
							Util.getImei(ProjectDetailActivity.this),
							new PostFollowProject.SuccessCallback() {

								@Override
								public void onSuccess(String msg) {
									Toast.makeText(
											ProjectDetailActivity.this,
											getResources().getString(
													R.string.attention_success),
											Toast.LENGTH_SHORT).show();
									mBtnAttention.setText(getResources()
											.getString(
													R.string.cancel_attention));
									if (mToFragment != null) {
										mToFragment.passValue();
									}
								}
							}, new PostFollowProject.FailCallback() {

								@Override
								public void onFail(String msg) {
									Toast.makeText(ProjectDetailActivity.this,
											msg, Toast.LENGTH_SHORT).show();
								}
							}, ProjectDetailActivity.this);
				} else {
					Toast.makeText(
							this,
							getResources().getString(
									R.string.check_your_network),
							Toast.LENGTH_SHORT).show();
				}
			} else {
				if (NetworkUtil.isNetworkAvailable(ProjectDetailActivity.this)) {
					new PostUnFollowProject(PreferencesUtil.getString(
							ProjectDetailActivity.this, "token"),
							mProjectItem.getProjectId(),
							PreferencesUtil.getString(
									ProjectDetailActivity.this, "userId"),
							Config.SOURCE,
							Util.getImei(ProjectDetailActivity.this),
							new PostUnFollowProject.SuccessCallback() {

								@Override
								public void onSuccess(String msg) {
									Toast.makeText(
											ProjectDetailActivity.this,
											getResources()
													.getString(
															R.string.cancel_attention_success),
											Toast.LENGTH_SHORT).show();
									mBtnAttention
											.setText(getResources()
													.getString(
															R.string.tab_title_attention));
									if (mToFragment != null) {
										mToFragment.cancelAttention();
									}
								}
							}, new PostUnFollowProject.FailCallback() {

								@Override
								public void onFail(String msg) {
									Toast.makeText(ProjectDetailActivity.this,
											msg, Toast.LENGTH_SHORT).show();
								}
							}, ProjectDetailActivity.this);
				} else {
					Toast.makeText(
							this,
							getResources().getString(
									R.string.check_your_network),
							Toast.LENGTH_SHORT).show();
				}
			}

			break;
		case R.id.btn_renchou:
			Intent intent = null;
			switch (Integer.valueOf(mProjectItem.getProjectStatus())) {
			case 0:
				intent = new Intent(ProjectDetailActivity.this,
						ProjectFundActivity.class);
				intent.putExtra("project", mBundle);
				this.startActivity(intent);
				break;
			case 1:
				intent = new Intent(ProjectDetailActivity.this,
						PayInformationActivity.class);
				intent.putExtra("project", mBundle);
				this.startActivity(intent);
				break;
			case 2:
			case 3:
				Toast.makeText(ProjectDetailActivity.this,
						getResources().getString(R.string.can_not_renchou),
						Toast.LENGTH_SHORT).show();
				break;
			}
			break;
		}
	}

	/**
	 * 设置点击Button的选中背景
	 * 
	 * @param index
	 */
	private void backColor(int index) {
		switch (index) {
		case 0:
			break;
		case 1:

			break;
		case 2:

			break;
		case 3:

			break;
		case 4:

			break;
		}
	}

	/**
	 * 分享功能
	 * 
	 * @param context
	 *            上下文
	 * @param activityTitle
	 *            Activity的名字
	 * @param msgTitle
	 *            消息标题
	 * @param msgText
	 *            消息内容
	 * @param imgPath
	 *            图片路径，不分享图片则传null
	 */
	public void shareMsg(String activityTitle, String msgTitle, String msgText,
			String imgPath) {
		Intent intent = new Intent(Intent.ACTION_SEND);
		if (imgPath == null || imgPath.equals("")) {
			intent.setType("text/plain"); // 纯文本
		} else {
			File f = new File(imgPath);
			if (f != null && f.exists() && f.isFile()) {
				intent.setType("image/jpg");
				Uri u = Uri.fromFile(f);
				intent.putExtra(Intent.EXTRA_STREAM, u);
			}
		}
		intent.putExtra(Intent.EXTRA_SUBJECT, msgTitle);
		intent.putExtra(Intent.EXTRA_TEXT, msgText);
		intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		startActivity(Intent.createChooser(intent, activityTitle));
	}

	/**
	 * Activity与Fragment的公用接口，用于传值
	 * 
	 * @author zhouchao
	 * 
	 */
	public interface ToFragment {
		void passValue();

		void cancelAttention();
	}

}
